#include <stdio.h>
#include <string.h>

void mergeFile(FILE *p1,FILE *p2, char *index)
{
	fseek(p1,0L,SEEK_END);
	int ch=fgetc(p2);

    int i = 0;

	while(!feof(p2))
	{
        if(index == NULL && i < 3){
            i++;
            ch=fgetc(p2);
            continue;
        }
		fputc(ch,p1);
		ch=fgetc(p2);
	}
}

int main()
{
    FILE *fp0,*fp1;
    char filename0[70], filename1[70];

    const char TXT[5] = ".txt";

    printf("Please input the part0 file:");
    scanf("%s",filename0);

    printf("Please input the part1 file:");
    scanf("%s",filename1);

    fp0=fopen(filename0,"rb+");
    if (fp0 == NULL)
    {
        printf("The part0 file is not exist! Press any key to exit\n");
        getch();
        return 0;
    }

    fp1=fopen(filename1,"rb");
    if (fp1 == NULL)
    {
        printf("The part1 file is not exist! Press any key to exit\n");
        getch();
        return 0;
    }


   char *index = strstr(filename1, TXT);


    mergeFile(fp0,fp1,index);

    fclose(fp0);
    fclose(fp1);

    printf("The file is merged successfully. Press any key to exit\n");
    getch();

    return 0;
}
